
// merge 1:1 ts_id using "$datapath\Eleanor\Control Groups\controlgroup", nogen keepusing(level* repeat*)



clear all
set more off, perm
capture log close

set seed 88

set maxvar 32767
//global klmChinaReach2: env klmChinaReach2
//global datapath "${klmChinaReach2}/Fuyao_Erlfang"
cd "/Volumes/klmChinaReach2/Haihan"

use "data_task_panel_cluster_May31.dta", clear
capture drop _merge
sort KIDID 


forvalues j=2/11{
	bys KIDID: egen temp`j'= max(paratel) if levell==`j' 
	bys KIDID: egen ave_pass_l`j'=max(temp`j') if levell!=.
	drop temp`j'
}
cap drop ave_pass_l_l
gen ave_pass_l_l =.
forvalues j=2/11{
	replace ave_pass_l_l=ave_pass_l`j' if levell==`j' & ave_pass_l_l ==.
}



collapse (mean) ave_pass_l_l ave_pass_l2 ave_pass_l3 ave_pass_l4 ave_pass_l5 ave_pass_l6 ave_pass_l7 ave_pass_l8 ave_pass_l9 ave_pass_l10 ave_pass_l11, by(KIDID levell)

keep if levell==1

save "rv_across_level_l.dta",replace

cd "/Volumes/klmChinaReach2/Haihan"

foreach s in l{
tempfile `s'_across_level
tempname table
postfile `table' str8(Lang) str8(quantile) str8(Category) G3 G4 G5 G6 G7 G8 G9 G10 G11 using `s'_across_level_q2, replace

forvalues i=2/10{
	local k=`i'+1
	forvalues j=`k'/11{
		count if ave_pass_l`i'>=0.25 & ave_pass_l`i'<0.5 & ave_pass_l`j'>=0.75 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local  l_q21_`i'_`j'=`r(N)'
		count  if ave_pass_l`i'>=0.25 & ave_pass_l`i'<0.5 & ave_pass_l`j'<0.75  & ave_pass_l`j'>=0.5  & ave_pass_l`i'!=. & ave_pass_l`j'!=.
	    local l_q22_`i'_`j'=`r(N)'
		count if ave_pass_l`i'>=0.25 & ave_pass_l`i'<0.5 & ave_pass_l`j'<0.5   & ave_pass_l`j'>=0.25 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local l_q23_`i'_`j'=`r(N)'
		count if ave_pass_l`i'>=0.25 & ave_pass_l`i'<0.5 & ave_pass_l`j'<0.25  & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local l_q24_`i'_`j'=`r(N)'
		count if ave_pass_l`i'>=0.25 & ave_pass_l`i'<0.5 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local N_q2_`i'_`j'=`r(N)'
		
	}
	
     if (`i'>2){
	 	forvalues jj=3/`i'{
			local l_q21_`i'_`jj'=-99
			local l_q22_`i'_`jj'=-99
			local l_q23_`i'_`jj'=-99
			local l_q24_`i'_`jj'=-99
			local N_q2_`i'_`jj'=-99
		}
	 }
	
	post `table' ("`i'") ("2") ("q21") (`l_q21_`i'_3') (`l_q21_`i'_4') (`l_q21_`i'_5') (`l_q21_`i'_6') (`l_q21_`i'_7') (`l_q21_`i'_8') (`l_q21_`i'_9') (`l_q21_`i'_10') (`l_q21_`i'_11')
	post `table' ("`i'") ("2") ("q22") (`l_q22_`i'_3') (`l_q22_`i'_4') (`l_q22_`i'_5') (`l_q22_`i'_6') (`l_q22_`i'_7') (`l_q22_`i'_8') (`l_q22_`i'_9') (`l_q22_`i'_10') (`l_q22_`i'_11')
	post `table' ("`i'") ("2") ("q23") (`l_q23_`i'_3') (`l_q23_`i'_4') (`l_q23_`i'_5') (`l_q23_`i'_6') (`l_q23_`i'_7') (`l_q23_`i'_8') (`l_q23_`i'_9') (`l_q23_`i'_10') (`l_q23_`i'_11')
	post `table' ("`i'") ("2") ("q24") (`l_q24_`i'_3') (`l_q24_`i'_4') (`l_q24_`i'_5') (`l_q24_`i'_6') (`l_q24_`i'_7') (`l_q24_`i'_8') (`l_q24_`i'_9') (`l_q24_`i'_10') (`l_q24_`i'_11')
	post `table' ("`i'") ("2") ("N")  (`N_q2_`i'_3') (`N_q2_`i'_4') (`N_q2_`i'_5') (`N_q2_`i'_6') (`N_q2_`i'_7') (`N_q2_`i'_8') (`N_q2_`i'_9') (`N_q2_`i'_10') (`N_q2_`i'_11')
}

postclose `table'

}








